<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Array Set</title>
</head>
<body>
  <script>
    // 创建长度为 8 的 int16 数组
    const container = new Int16Array(8);
    // 把定型数组复制为前 4 个值
    // 偏移量默认为索引 0
    container.set(Int8Array.of(1, 2, 3, 4));
    console.log(container);  // Int16Array(8) [1, 2, 3, 4, 0, 0, 0, 0]
    container.set([5, 6, 7, 8], 4);
    console.log(container);  // Int16Array(8) [1, 2, 3, 4, 5, 6, 7, 8]
    // 溢出会抛错
    // container.set([5, 6, 7, 8], 7); // RangeError
  </script>

  <script>
    const source = Int16Array.of(2, 4, 6, 8);

    // 把整个数组复制为一个同类型的新数组
    const fullCopy = source.subarray();
    console.log(fullCopy);  // Int16Array(4) [2, 4, 6, 8]

    // 从索引 2 开始复制数组
    const halfCopy = source.subarray(2);
    console.log(halfCopy);  // Int16Array(2) [6, 8]
    
    // 从索引 1 开始复制到索引 3
    const partialCopy = source.subarray(1, 3);
    console.log(partialCopy);  // Int16Array(2) [4, 6]
  </script>
</body>
</html>